package io.seata.sample.controller;

import io.seata.sample.entity.Order;
import io.seata.sample.service.OrderService;

import java.math.BigDecimal;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author IT云清
 */
@RestController
@RequestMapping(value = "order")
public class OrderController {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private OrderService orderServiceImpl;


    private static final AtomicInteger count = new AtomicInteger(0);

    /**
     * 创建订单
     *
     * @param order
     * @return
     */
    @GetMapping("create")
    public String create(Order order) {

        long start = System.currentTimeMillis();
        LOGGER.info("开始创建订单 count={} start={}", count.incrementAndGet(), start);
        orderServiceImpl.create(order);
        long end = System.currentTimeMillis();
        LOGGER.info("结束创建订单 start={} 耗时={}", end, end - start);
        return "Create order success";

    }

    /**
     * 修改订单状态
     *
     * @param userId
     * @param money
     * @param status
     * @return
     */
    @RequestMapping("update")
    String update(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money, @RequestParam("status") Integer status,@RequestParam("orderId") Long orderId) {
        orderServiceImpl.update(userId, money, status,orderId);
        return "订单状态修改成功";
    }
}
